package com.hxj.docking.service.impl;

import com.hxj.docking.pojo.dto.DataDTO;
import com.hxj.docking.service.AsyncTask;
import com.hxj.docking.service.DockingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author hxj
 * @Date 2024/11/19 10:36
 * @Description
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class DockingServiceImpl implements DockingService {

    private final AsyncTask asyncTask;

    @Override
    public void startDocking() {
        // 获取数据
        List<DataDTO> dataList = getData();

        // 异步对接
        for (DataDTO dataMap : dataList) {
            // 开启线程
            try {
                asyncTask.processDataAsync(dataMap);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }



    }

    private List<DataDTO> getData() {
        List<String> head = Arrays.asList("A-01", "A-02", "A-02", "A-03", "A-03");
        List<DataDTO> list = new ArrayList<>();
        int count = 1;
        for (int i = 0; i < 5; i++) {
            DataDTO dataDTO = new DataDTO();
            dataDTO.setId(count);
            dataDTO.setNo("a-" + count);
            dataDTO.setHead(head.get(i));
            list.add(dataDTO);
            count++;
        }
        return list;
    }



}
